﻿create PROCEDURE Sync.SP_Customers_SelectChanges
@LastRowVersion rowversion,
@FilterdColumn UNIQUEIDENTIFIER
AS 
BEGIN 
	SELECT 
        t.[CustomerID],
		g.[CustomerName],g.[Birthdate],g.[Gender],g.[SSN],g.[SSNCreationDate],g.[SSNCreationLocationID],g.[BirthLocationID],g.[Address],g.[CurrentJob],g.[MilitaryStatusID],g.[SocialStatusID],g.[EducationalLevelID],g.[MilitaryPostpondingDate],
		t.IsDeleted,
		t.LastUpdatedDate
	FROM [dbo].[Customers] g RIGHT JOIN Sync.Customers_Tracking t  
    ON t.CustomerID = g.CustomerID 
    join [CustomerLoans] cl
			on (g.CustomerID = cl.CustomerID or g.CustomerID = cl.GuarantorID)
	WHERE (@LastRowVersion IS NULL OR t.Version > @LastRowVersion)
	AND ((@FilterdColumn IS NULL) OR (cl.CommitteeID = @FilterdColumn))
    END














